package cn.ronghuanet.auth.controller;

import cn.ronghuanet.auth.annotation.RonghuaPermission;
import cn.ronghuanet.auth.domain.Permission;
import cn.ronghuanet.auth.query.PermissionQuery;
import cn.ronghuanet.auth.service.IPermissionService;
import cn.ronghuanet.basic.util.AjaxResult;
import cn.ronghuanet.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/permission")
@RonghuaPermission(name = "权限管理")
public class PermissionController {

    @Autowired
    private IPermissionService permissionService;

    @PostMapping
    @RonghuaPermission(name = "分页查询")
    public AjaxResult pageList(@RequestBody PermissionQuery query){
        try {
            PageList<Permission> pageList = permissionService.pageList(query);
            return AjaxResult.me().setResultObj(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("权限分页查询失败!"+e.getMessage());
        }
    }

    /**
     * 一级权限和他们各自的二级权限
     * @return
     */
    @GetMapping("/tree")
    public AjaxResult getTree(){
        try {
            // 这是返回的一级权限
            List<Permission> permissions = permissionService.getTree();
            return AjaxResult.me().setResultObj(permissions);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("权限树查询失败!"+e.getMessage());
        }
    }

    @GetMapping("/sns")
    public AjaxResult getPermisionSns(){
        try {
            List<String> permisionSns = permissionService.getPermisionSns();
            return AjaxResult.me().setResultObj(permisionSns);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("权限树查询失败!"+e.getMessage());
        }
    }
}
